<?php
/**
 * Created by PhpStorm.
 * User: serafina
 * Date: 2015/9/23
 * Time: 09:50
 */

function mobile_service_plant_structure_list($parameters){
    global $user;
    $query = db_select('addr', 'A');//地址表
    $query->join('plant_structure', 'PS', 'A.addr_id=PS.addr_id');//内连接 成一个表
    $query->leftJoin('plant', 'P1', 'PS.plant_id=P1.plant_id');//需要得到植物名称
    $query->fields('PS');
    $query->addField('P1', 'plant_name');

    //地址级别查询
    if (!empty($parameters['county'])) {//县级
        $query->condition('A.county_id', $parameters['county'], '=');
    } elseif (!empty($parameters['city'])) {//市级
        $query->condition('A.city_id', $parameters['city'], '=');
    } elseif (!empty($parameters['province'])) {//省级
        $query->condition('A.province_id', $parameters['province'], '=');
    }
    //模糊条件筛选
    if (!empty($parameters['plant'])) {//模糊植物名字查询条件
        $query->condition('P1.plant_name', '%' . db_like($parameters['plant']) . '%', 'LIKE');
    }
    if (!empty($parameters['year'])) {//模糊年度查询条件
        $query->condition('PS.year', '%' . db_like($parameters['year']) . '%', 'LIKE');
    }

    $query->leftJoin('addr', 'A1', 'A.province_id=A1.addr_id');
    $query->leftJoin('addr', 'A2', 'A.city_id=A2.addr_id');
    $query->leftJoin('addr', 'A3', 'A.county_id=A3.addr_id');
    $query->addField('A1', 'name', 'province');
    $query->addField('A2', 'name', 'city');
    $query->addField('A3', 'name', 'county');
    if(!user_access('view all plant_structure') && user_access('view own plant_structure')){
        $query->condition('PS.author_uid',$user->uid, '=');
    }
    $query->orderBy('PS.plant_structure_id', 'DESC');
    if (!empty($parameters['cursor']) && !empty($parameters['comparator'])) { //游标翻页
        $query->condition('PS.plant_structure_id', $parameters['cursor'], $parameters['comparator']);
    }
    $query->range(0,10);
    
    $results = $query->execute();
    $plant_structure_list = [];
    if(!empty($results)){
        foreach ($results as $result) {
            $plant_structure_list[] = $result;
        }
    }
    $object_plant_structure_list = (object)["data"=>$plant_structure_list];
    return  $object_plant_structure_list;
}

function mobile_service_plant_structure_add($parameters){
    global $user;
    $module_name="plant_structure";
    $id = !empty($parameters['plant_structure_id'])?$parameters['plant_structure_id']:'';
    if (empty($id)) { //add new brand_sales
        if(!user_access('add '.$module_name)){
            return ['option'=>0,'errorNum'=>3];
        }
        $nid = db_insert('plant_structure') // Table name no longer needs {}
        ->fields(array(
            'addr_id' => $parameters['addr_id'],
            'plant_id' => $parameters['plant_id'],
            'year' => $parameters['year'],
            'quantity' => $parameters['quantity'],
            'author_uid' => $user->uid,
            'timestamp' => time(),))
            ->execute();
            $result_option = (object)["option" => 1];
            return $result_option;
    } else {
        if(!mobile_user_edit_access($module_name,$module_name.'_id',$id,'edit own '.$module_name,'edit all '.$module_name)){
            return ['option'=>0,'errorNum'=>3];
        }
        $nid = db_update('plant_structure')
        ->fields(array(
            'addr_id' => $parameters['addr_id'],
            'plant_id' => $parameters['plant_id'],
            'year' => $parameters['year'],
            'quantity' => $parameters['quantity'],
            'timestamp' => time(),))
            -> condition('plant_structure_id',$id, '=')
            ->execute();
        if($nid > 0){
            $result_option = (object)["option" => 1];
            return $result_option;
        }
        $result_option = (object)["option" => 0];
        return $result_option;
    }
}